package org.gdsdxy.pmmp.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.gdsdxy.pmmp.entity.MemberBaseInfo;
import org.gdsdxy.pmmp.entity.MemberImge;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class UserManagerDaoImplByHbn extends HibernateDaoSupport implements IUserManagerDao {

	@Override
	public long getImageCountById(Integer id) {
		long count = 0;
		String hql = "select count(*) from MemberImge i where i.baseInfo.id = ?";
		List list = this.getHibernateTemplate().find(hql.toString(),new Object[]{id});
		if(list!=null&&list.size()>0){
			count = (Long) list.get(0);
		}
		return count;
	}

	@Override
	public List<MemberImge> queryImgeByUserId(Integer id) {
		String hql="from MemberImge i where i.baseInfo.id = ?";
		return this.getHibernateTemplate().find(hql.toString(),new Object[]{id});
	}

	@Override
	public List<MemberImge> queryImgeByUserIdFy(final int start,final int size, final Integer id) {
		final String hql = "from MemberImge where userid=?";
		List images = (List)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setInteger(0, id);
				query.setFirstResult(start);
				query.setMaxResults(size);
				return query.list();
			}
			
			
		});
		return images;
	}

	@Override
	public MemberBaseInfo queryMemberBaseInfo(String userName, String password) {
		String hql = "from MemberBaseInfo m where m.userName=? and password = ?";
		List list = this.getHibernateTemplate().find(hql,new Object[]{userName,password});
		return (list==null||list.size()==0)?null:(MemberBaseInfo)list.get(0);
	}

	@Override
	public List<MemberBaseInfo> queryUsers(final int start,final int size) {
		final String hql = "from MemberBaseInfo";
		return (List)this.getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(start);
				query.setMaxResults(size);
				return query.list();
			}
			
		});
	}

	@Override
	public boolean saveMemberImge(MemberImge imge) {
		Serializable id =  this.getHibernateTemplate().save(imge);
		System.out.println("ID:"+id);
		return  id==null ? false:true;
	}

	@Override
	public boolean saveMemberBaseInfo(MemberBaseInfo baseInfo) {
		Serializable id = this.getHibernateTemplate().save(baseInfo);
		return id==null?false:true;
	}

  public MemberBaseInfo get(Integer id) {
    // TODO Auto-generated method stub
    return (MemberBaseInfo) this.getHibernateTemplate().get(MemberBaseInfo.class, id);
  }

}
